package com.wnnight.wys.web.car;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wnnight.wys.entity.car.CarInOut;
import com.wnnight.wys.entity.car.Park;
import com.wnnight.wys.service.car.CarInOutService;
import com.wnnight.wys.vo.ResultVo;
import com.wnnight.wys.vo.car.CarDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @ClassName: CarInOutController
 * @Description: CarInOut控制器
 * @author: CHX42
 * @date: 2022/8/28-18:42
 * @version: v1.0
 */
@RestController
@RequestMapping("/api/v1/car/carinout")
public class CarInOutController {
    @Autowired
    private CarInOutService carInOutService;

    @PostMapping("/list")
    @PreAuthorize("hasAuthority('car:cario:list')")
    public ResultVo<?> list(@RequestBody CarDataVo<CarInOut,CarInOut> carDataVo){
        QueryWrapper<Park> parkWrapper = new QueryWrapper<>();
        String startInTime = carDataVo.getData().getStartInTime();
        String endInTime = carDataVo.getData().getEndInTime();
        String startOutTime = carDataVo.getData().getStartOutTime();
        String endOutTime = carDataVo.getData().getEndOutTime();
        String carNum=carDataVo.getData().getCarNum();
        parkWrapper.ge(StringUtils.hasText(startInTime),"wys_car_in_out.in_time",startInTime);
        parkWrapper.le(StringUtils.hasText(startOutTime),"wys_car_in_out.in_time",startOutTime);
        parkWrapper.ge(StringUtils.hasText(endInTime),"wys_car_in_out.out_time",endInTime);
        parkWrapper.le(StringUtils.hasText(endOutTime),"wys_car_in_out.out_time",endOutTime);
        parkWrapper.like(StringUtils.hasText(carNum),"wys_car_in_out.car_num",carNum);
        Page<CarInOut> backCarInOut = carInOutService.getAll(new Page<>(carDataVo.getPagePlus().getCurrent(), carDataVo.getPagePlus().getSize()), parkWrapper);
        return new ResultVo<>(backCarInOut);
    }
    @PostMapping("/download")
//    @PreAuthorize("hasAuthority('car:cario:dlod')")
    public void downLoad(@RequestBody CarDataVo<CarInOut,CarInOut> carDataVo,HttpServletResponse response) throws IOException {
        QueryWrapper<Park> parkWrapper = new QueryWrapper<>();
        String startInTime = carDataVo.getData().getStartInTime();
        String endInTime = carDataVo.getData().getEndInTime();
        String startOutTime = carDataVo.getData().getStartOutTime();
        String endOutTime = carDataVo.getData().getEndOutTime();
        String carNum=carDataVo.getData().getCarNum();
        parkWrapper.ge(StringUtils.hasText(startInTime),"wys_car_in_out.in_time",startInTime);
        parkWrapper.le(StringUtils.hasText(startOutTime),"wys_car_in_out.in_time",startOutTime);
        parkWrapper.ge(StringUtils.hasText(endInTime),"wys_car_in_out.out_time",endInTime);
        parkWrapper.le(StringUtils.hasText(endOutTime),"wys_car_in_out.out_time",endOutTime);
        parkWrapper.like(StringUtils.hasText(carNum),"wys_car_in_out.car_num",carNum);
        List<CarInOut> records =(List) carInOutService.listObjs();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("车辆进出表", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), CarInOut.class).sheet("车辆进出表").doWrite(records);
    }


}
